package kr.co.medical.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import kr.co.medical.dao.NoticeDAO;
import kr.co.medical.dto.Notice;
import kr.co.medical.dto.RowNumDTO;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class NoticeController {
	@Autowired
	private SqlSession sqlSession;

	// 게시판 전체 수
	public int getCount() throws ClassNotFoundException, SQLException {
		int count = 0;
		NoticeDAO noticeDao = sqlSession.getMapper(NoticeDAO.class);
		count = noticeDao.getCount();
		return count;
	}

	// 페이지 사이즈, 페이지 그룹
	private final int PAGESIZE = 10;
	private final int PAGEGROUP = 10;

	@RequestMapping("notice.do")
	public ModelAndView getNotices(String pageNumber)
			throws ClassNotFoundException, SQLException {

		ModelAndView mav = new ModelAndView();
		List<Notice> list = null;

		// 현재 클릭 페이지
		int pageNum = 1;
		if (pageNumber != null)
			pageNum = Integer.parseInt(pageNumber);
		int totalCount = getCount();
		System.out.println("총 개시물수" + totalCount);

		// 페이지 갯수
		int totalPageCount = totalCount / PAGESIZE;
		// 0으로 나눠 떨어지지 않을경우 페이지 갯수를 +1한다.
		if (totalCount % PAGESIZE != 0) {
			totalPageCount++;
		}
		// startPage or endPage
		int startPage = (pageNum - 1) / PAGEGROUP * PAGEGROUP + 1;
		int endPage = startPage + (PAGEGROUP - 1);
		
		if (endPage > totalPageCount) {
			endPage = totalPageCount;
		}

		// 마지막, 처음 rowNumber 선언 및 초기화
		int endRow = PAGESIZE * pageNum;
		int startRow = endRow - PAGESIZE + 1;

		// //////////////////////////////////////////////////////////
		System.out.println("인터페이스를 구현한 Mapper 제공 : 아래 코드");
		NoticeDAO noticeDao = sqlSession.getMapper(NoticeDAO.class);
		// //////////////////////////////////////////////////////////
		System.out.println(startRow);
		System.out.println(endRow);
		
		RowNumDTO rowNumDTO = new RowNumDTO();
		rowNumDTO.setStartRow(startRow);
		rowNumDTO.setEndRow(endRow);
		list = noticeDao.getNotices(rowNumDTO);
		mav.addObject("boardCount", list.size());
		mav.addObject("totalPageCount", totalPageCount);
		mav.addObject("startPage", startPage);
		mav.addObject("endPage", endPage);
		mav.addObject("pageNumber", pageNum);
		mav.addObject("list", list);
		mav.setViewName("patient.notice");
		
		return mav;
	}

	@RequestMapping("noticeDetail.do")
	public String noticeDetail(String notice_id, Model model)
			throws ClassNotFoundException, SQLException {
		NoticeDAO noticeDao = sqlSession.getMapper(NoticeDAO.class);
		Notice notice = noticeDao.getNotice(notice_id);
		model.addAttribute("notice", notice);
		
		return "patient.noticeDetail";
	}

	// 글 삭제
	@RequestMapping("noticeDel.do")
	public String noticeDel(String notice_id) throws ClassNotFoundException,
			SQLException {
		NoticeDAO noticeDao = sqlSession.getMapper(NoticeDAO.class);
		noticeDao.delete(notice_id);
		return "redirect:notice.do";
	}

	// 글등록화면(view)
	@RequestMapping(value = "noticeWrite.do", method = RequestMethod.GET)
	public String noticeReg() {
		return "patient.noticeWrite";
	}

	// 글등록처리(model)
	@RequestMapping(value = "noticeWrite.do", method = RequestMethod.POST)
	public String noticeReg(Notice n, HttpServletRequest request)
			throws IOException, ClassNotFoundException, SQLException {
		System.out.println("실제 글 등록 처리");
		
		NoticeDAO noticeDao = sqlSession.getMapper(NoticeDAO.class);
		noticeDao.insert(n);

		return "redirect:notice.do";
		/////////////////////////////////////////////////////////////////
	}

	// 글수정하기(GET : 수정화면, POST : Data처리->update)
	@RequestMapping(value = { "noticeEdit.do" }, method = RequestMethod.GET)
	public String noticeEdit(String notice_id, Model model)
			throws ClassNotFoundException, SQLException {
		NoticeDAO noticeDao = sqlSession.getMapper(NoticeDAO.class);
		Notice notice = noticeDao.getNotice(notice_id);
		model.addAttribute("notice", notice);

		return "patient.noticeEdit";
	}

	@RequestMapping(value = { "noticeEdit.do" }, method = RequestMethod.POST)
	public String noticeEdit(Notice notice, HttpServletRequest request)
			throws ClassNotFoundException, SQLException, IOException {
		NoticeDAO noticeDao = sqlSession.getMapper(NoticeDAO.class);
		noticeDao.update(notice);
		return "redirect:noticeDetail.do?notice_id=" + notice.getNotice_id();
	}
}